import java.util.ArrayList;
import java.util.Date;
public class Statistic {
    private ArrayList<Page> pageList;

    public Statistic() {
        pageList = new ArrayList<>();
    }

    public void recordPageVisit(String url) {
        Page targetPage = null;
        for (Page page : pageList) {
            if (page.getUrl().equals(url)) {
                targetPage = page;
                break;
            }
        }
        if (targetPage == null) {
            targetPage = new Page(url);
            pageList.add(targetPage);
        }
        targetPage.NewCount();
    }

    public void VisitRank() {
        pageList.sort((p1, p2) -> p2.getVisitCount() - p1.getVisitCount());
        System.out.println("排行\t"+"URL\t"+"访问量");
        for (int i = 0; i < pageList.size(); i++) {
            Page page = pageList.get(i);
            System.out.println((i + 1) +"\t"+ page.getUrl() +"\t" + page.getVisitCount());
        }
    }

    public int calculateTotalVisitCount() {
        int total = 0;
        for (Page page : pageList) {
            total += page.getVisitCount();
        }
        return total;
    }

    public double calculateAverageVisitCount() {
        if (pageList.isEmpty()) {
            return 0;
        }
        int total = calculateTotalVisitCount();
        return (double) total / pageList.size();
    }

    public Date getLastVisitTime(String url) {
        for (Page page : pageList) {
            if (page.getUrl().equals(url)) {
                return page.getLastVisitTime();
            }
        }
        return null;
    }

    public void clearStatistics() {
        pageList.clear();
    }
}
